Conditional (computer programming)
part 9/26 · 46.2 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Many languages support conditional expressions, which are similar to if statements, but return a value as a result. Thus, they are true expressions (which evaluate to a value), not statements (which may not be permitted in the context of a value). The concept of conditional expressions was first developed by John McCarthy during his research into symbolic processing and LISP in the late 1950s.
Algol family
myvariable := if x > 20 then 1 else 2
Lisp dialects
Conditional expressions have always been a fundamental part of Lisp . In pure LISP, the COND function is used. In dialects such as Scheme, Racket and Common Lisp :
;; Scheme
(define (myvariable x) (if (> x 12) 1 2)) ; Assigns 'myvariable' to 1 or 2, depending on the value of 'x'
;; Common Lisp
(let ((x 10))
(setq myvariable (if (> x 12) 2 4))) ; Assigns 'myvariable' to 2
Haskell